/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.soc.cs5226.gui.controller;

import edu.soc.cs5226.gui.entities.*;
import edu.soc.cs5226.gui.entities.util.TimeUtil;
import edu.soc.cs5226.gui.entities.util.Constants;
import java.io.Serializable;  
import java.sql.Timestamp;
import java.util.Iterator;
import java.util.List;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ManagedBean;
  
import org.primefaces.model.DefaultTreeNode;  
import org.primefaces.model.TreeNode;  
  
/**
 * 
 *
 * @author Huijia
 */
  
@ManagedBean
@SessionScoped
public class BufferCacheController implements Serializable{
    private DAO openDAO;
    private TreeNode root;
    private InitParameter spParamObjBuffer;
    private InitParameter spParamObjSize;
    private InitParameter spParamObjCache;
    
    private static final String BUFFERCACHE_PAGE = "buffercache.xhtml";
        
    public BufferCacheController() 
    {  
        openDAO = new DAO();
        obtainInitParameters();
    }
	
    public TreeNode getRoot() 
    {
        return root;
    }

    public InitParameter getSpParamObjBuffer() {
        return spParamObjBuffer;
    }

    public void setSpParamObjBuffer(InitParameter spParamObjBuffer) {
        this.spParamObjBuffer = spParamObjBuffer;
    }

    public InitParameter getSpParamObjCache() {
        return spParamObjCache;
    }

    public void setSpParamObjCache(InitParameter spParamObjCache) {
        this.spParamObjCache = spParamObjCache;
    }

    public InitParameter getSpParamObjSize() {
        return spParamObjSize;
    }

    public void setSpParamObjSize(InitParameter spParamObjSize) {
        this.spParamObjSize = spParamObjSize;
    }
    
    public void obtainInitParameters() {
        try{
            List<InitParameter> initObjList = openDAO.getInitParam2(Constants.BUFFERCACHEBUFFERINIT, Constants.BUFFERCACHESIZEINIT);
            Iterator iter = initObjList.iterator();
            while (iter.hasNext())
            {
                InitParameter ipObj = (InitParameter)iter.next();
                if (ipObj.getName().contains("buffers"))
                {
                    setSpParamObjBuffer(ipObj);
                }
                
                else if (ipObj.getName().contains("size"))
                {
                    setSpParamObjSize(ipObj);
                }
            }
            
            InitParameter ipObjCache = new InitParameter();
            ipObjCache.setName("Buffer Cache Size = db_block_buffers x db_block_size = ");
            ipObjCache.setValue((Long)spParamObjBuffer.getValue()*(Long)spParamObjSize.getValue());
            setSpParamObjCache(ipObjCache);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public String processViewMore() 
    {
        try
        {
            root = new DefaultTreeNode("root",null);
            Timestamp now = new Timestamp(TimeUtil.getInstance().getTime());
            List<DBParameter> buffercacheList = openDAO.getbcList(now);
            if (!buffercacheList.isEmpty())
            {
                HealthStatus thresholds = openDAO.getBCHealthStatus();

                CalculateXY calcXY = new CalculateXY();
                List<xObject> xList = calcXY.process(buffercacheList, thresholds, Constants.BUFFERCACHE);
                dayObject dayObj = new BufferCacheProcessList().processTreeNode(now, xList, thresholds);

                TreeNode treenode24 = new DefaultTreeNode((BufferCache)dayObj.getRootObj(),root);
                
                Iterator<xObject> xIter = dayObj.getxObjects().iterator();
                while (xIter.hasNext())
                {
                    xObject currX = xIter.next();
                    BufferCache spObjX = (BufferCache)currX.getxObj();

                    TreeNode treeNodeX = new DefaultTreeNode(spObjX,treenode24);

                    Iterator<yObject> yIter = currX.getyObjects().iterator();
                    while (yIter.hasNext())
                    {
                        yObject currY = yIter.next();
                        BufferCache spObjY = (BufferCache)currY.getyObj();

                        TreeNode treeNodeY = new DefaultTreeNode(spObjY,treeNodeX);

                        Iterator<DBParameter> bcIter = currY.getObj().iterator();
                        while (bcIter.hasNext())
                        {
                            BufferCache bc = (BufferCache)bcIter.next();

                            TreeNode treeNodeBC = new DefaultTreeNode(bc,treeNodeY);
                        }
                    }
                }
            } // if (!buffercacheList.isEmpty())
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return BUFFERCACHE_PAGE;
    } // processViewMore
}
